<?php
/**
 * 系统公共类
 * @version 1.0
 * @author 许跃华
 * =========================================================================================
 * @abstract searchSendTypeAll() 查询配送方式信息
 * @abstract searchSendRegion()  查询配送区域
 * =========================================================================================
 */

class systemHandler {
	 
	private $dbca;
	
	private $configContent;
	private $configFile;
	private $configFileName;
	
	public function __construct( $dbca=null ) {
		$this->dbca = $dbca;
	}

    /**
     * 查询支付方式
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'payTypeID' => int 支付方式ID
     * )
     * @return null
     */
    public function searchOnlinepPrameter( $arg_array ) {

        if (!isset($arg_array['payTypeID']) || !is_numeric($arg_array['payTypeID'])) {
            return null;
        }

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //支付方式ID
        $sql .= ' AND payTypeID = ?';
        $params_type .= 'i';
        $params_array[] = $arg_array['payTypeID'];

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_online_parameter ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array = array(
                    'payTypeID' => $item->payTypeID,
                    'payTypeName' => $item->payTypeName,
                    'partnerID' => $item->partnerID,
                    'seller_email' => $item->seller_email,
                    'seller_name' => $item->seller_name,
                    'service' => $item->service,
                    'md5_key' => $item->md5_key,
                    'private_key' => $item->private_key,
                    'public_key' => $item->public_key,
                    'sign_type' => $item->sign_type,
                    'charset' => $item->charset,
                    'gate_url' => $item->gate_url,
                    'notify_url' => $item->notify_url,
                    'return_url' => $item->return_url,
                    'version' => $item->version,
                    'extend_1' => $item->extend_1,
                    'extend_2' => $item->extend_2,
                    'extend_3' => $item->extend_3,
                    'extend_4' => $item->extend_4,
                    'extend_5' => $item->extend_5,
                    'alias' => $item->alias,
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 查询支付方式
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchPayType( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //支付方式ID
        if (isset($arg_array['payTypeID']) && is_numeric($arg_array['payTypeID'])) {
            $sql .= ' AND payTypeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['payTypeID'];
        }

        //支付方式类型
        if (isset($arg_array['type']) && is_numeric($arg_array['type'])) {
            $sql .= ' AND type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['type'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //来源
        if (isset($arg_array['source'])) {
            if ($arg_array['source'] == 'web') {
                $sql .= ' AND b2c_usable = 1';
            } else if ($arg_array['source'] == 'wap') {
                $sql .= ' AND wap_usable = 1';
            } else if ($arg_array['source'] == 'wx') {
                $sql .= ' AND wx_usable = 1';
            } else if ($arg_array['source'] == 'app') {
                $sql .= ' AND app_usable = 1';
            }
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_paytype ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_paytype ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                if ($item->type == 1) {
                    $type_name = '货到付款';
                } else if ($item->type == 2) {
                    $type_name = '在线支付';
                } else if ($item->type == 3) {
                    $type_name = '汇款';
                } else if ($item->type == 4) {
                    $type_name = '余额';
                } else if ($item->type == 5) {
                    $type_name = '积分';
                }

                $item_array[] = array(
                    'payTypeID' => $item->payTypeID,
                    'payTypeName' => $item->payTypeName,
                    'logo_img' => $item->logo_img ? G_IMAGE_URL . $item->logo_img : '',
                    'memo' => $item->memo,
                    'useFlg' => $item->useFlg,
                    'type' => $item->type,
                    'type_name' => $type_name,
                    'payment_platform' => $item->payment_platform,
                    'is_default' => $item->is_default,
                    'b2c_usable' => $item->b2c_usable,
                    'wap_usable' => $item->wap_usable,
                    'wx_usable' => $item->wx_usable,
                    'app_usable' => $item->app_usable,
                    'external_id' => $item->external_id ? $item->external_id : $item->payTypeID
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询付款方式
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function searchPaymentMethod( &$arg_array ) {

        if (!isset($arg_array['payTypeID']) || !is_numeric($arg_array['payTypeID'])) {
            return null;
        }

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //支付方式ID
        $sql .= ' AND payTypeID = ?';
        $params_type .= 'i';
        $params_array[] = $arg_array['payTypeID'];

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_payment_method ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_payment_method ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();
            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'payTypeID' => $item->payTypeID,
                    'id' => $item->id,
                    'name' => $item->name,
                    'useFlg' => $item->useFlg,
                    'useName' => $item->useFlg ? LANG_ON : LANG_OFF
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询银行
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function searchBank( &$arg_array ) {

        if (!isset($arg_array['payTypeID']) || !is_numeric($arg_array['payTypeID'])) {
            return null;
        }

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //支付方式ID
        $sql .= ' AND payTypeID = ?';
        $params_type .= 'i';
        $params_array[] = $arg_array['payTypeID'];

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_bank ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_bank ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'payTypeID' => $item->payTypeID,
                    'bank_id' => $item->bank_id,
                    'bank_name' => $item->bank_name,
                    'bank_logo' => $item->bank_logo ? G_IMAGE_URL . $item->bank_logo : '',
                    'payee' => $item->payee,
                    'bank_account_numbers' => $item->bank_account_numbers,
                    'bank_location' => $item->bank_location,
                    'bank_branch' => $item->bank_branch,
                    'useFlg' => $item->useFlg,
                    'useName' => $item->useFlg ? LANG_ON : LANG_OFF
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
	
    /**
     * 根据配送方式查询支付方式类型
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sendTypeID' => int 配送方式ID
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchPayTypeFromSendPay( &$arg_array ) { 

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //配送方式ID
        if (isset($arg_array['sendTypeID']) && is_numeric($arg_array['sendTypeID'])) {
            $sql .= ' AND sendTypeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sendTypeID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_sendtype_pay ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_sendtype_pay ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                if ($item->pay_type == 1)
                    $item->pay_type_name = '货到付款';
                else if ($item->pay_type == 2)
                    $item->pay_type_name = '在线支付';
                else if ($item->pay_type == 3)
                    $item->pay_type_name = '汇款';
                $item_array[] = array(
                    'sendTypeID' => $item->sendTypeID,
                    'pay_type' => $item->pay_type,
                    'pay_type_name' => $item->pay_type_name
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 订单结算
     * 根据收货地址查询配送方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function checkoutSendTypeList( &$bill_master_array, &$cust_buy_log = null ) {

        $item_array = array();

        //sql语句
        $search_sql = 'SELECT ss.sendTypeID,ss.sendTypeName,ss.description,ssa.send_region_id,ssa.send_region_name,ss.ziti
                       FROM sys_send_area_list ssal
                       INNER JOIN sys_send_area ssa ON ssa.send_region_id = ssal.send_region_id
                       INNER JOIN sys_sendtype ss ON ss.sendTypeID = ssa.sendTypeID AND ss.useFlg = 1
                       WHERE ssal.regionID  = ?';

        for( $i = 0; $i < 3; $i++ ) {

            if ($i == 0) {
                if (empty($bill_master_array['countyid'])) {
                    continue;
                }
                //根据地区查询
                $send_type_array = $this->dbca->stmt_query($search_sql, 'i', $bill_master_array['countyid']);
            } else if ($i == 1) {
                //根据市区查询
                $send_type_array = $this->dbca->stmt_query($search_sql, 'i', $bill_master_array['cityid']);
            } else if ($i == 2) {
                //根据省份
                $send_type_array = $this->dbca->stmt_query($search_sql, 'i', $bill_master_array['provinceid']);
            }

            if (!$send_type_array) {
                continue;
            }

            foreach($send_type_array as &$item) {

                if (isset($item_array[$item->send_region_id])) {
                    continue;
                }

                $item_array[$item->send_region_id] = array(
                    'sendTypeID' => $item->sendTypeID,
                    'sendTypeName' => $item->sendTypeName,
                    'send_region_id' => $item->send_region_id,
                    'send_region_name' => $item->send_region_name,
                    'description' => $item->description,
                    'ziti' => $item->ziti
                );

                if (!isset($bill_master_array['sendTypeID']) || (!is_null($cust_buy_log) && $item->send_region_id == $cust_buy_log['send_region_id'])) {
                    $bill_master_array['ziti'] = $item->ziti;
                    $bill_master_array['sendTypeID'] = $item->sendTypeID;
                    $bill_master_array['sendTypeName'] = $item->sendTypeName;
                    $bill_master_array['send_region_id'] = $item->send_region_id;
                    $bill_master_array['send_region_name'] = $item->send_region_name;
                    $bill_master_array['extend_1'] = null;
                    $bill_master_array['extend_9'] = null;
                    $bill_master_array['extend_10'] = null;
                    $bill_master_array['extend_2'] = null;
                    $bill_master_array['extend_11'] = null;
                }

            }


        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['send_region_id'] = $bill_master_array['send_region_id'];
        }

        return $item_array;

    }


    /**
     * 订单结算
     * 查询配送网点列表
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function checkoutBranchList( &$bill_master_array, &$cust_buy_log ) {

        //查询自提点
        $arg_array = array(
            'provinceid' => $bill_master_array['provinceid'],
            'cityid' => $bill_master_array['cityid'],
            'countyid' => $bill_master_array['countyid'],
            'useFlg' => 1
        );
        $branch_array = $this->searchBranchInfo( $arg_array );
        if (!$branch_array) {
            return null;
        }

        if (isset($bill_master_array['extend_1'])) {

            //循环自提点数组
            foreach($branch_array as &$branch) {
                if ($branch['branch_id'] == $cust_buy_log['extend_1']) {
                    $bill_master_array['extend_1'] = $branch['branch_id'];
                    $bill_master_array['extend_9'] = $branch['branch_name'];
                    $bill_master_array['extend_10'] = $branch['branch_code'];
                }
            }
            $cust_buy_log['extend_1'] = $bill_master_array['extend_1'];


        }

        return $branch_array;

    }

    /**
     * 订单结算
     * 查询自提点信息
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @param int $branch_id 自提点ID
     * @return array|null
     */
    public function checkoutBranch( &$bill_master_array, &$cust_buy_log, $branch_id ) {

        //查询自提点
        $arg_array = array(
            'branch_id' => $branch_id,
            'useFlg' => 1
        );
        $branch_array = $this->searchBranchInfo( $arg_array );
        if (!$branch_array) {
            return false;
        }

        $bill_master_array['extend_1'] = $branch_array[0]['branch_id'];
        $bill_master_array['extend_9'] = $branch_array[0]['branch_name'];
        $bill_master_array['extend_10'] = $branch_array[0]['branch_code'];

        $cust_buy_log['extend_1'] = $bill_master_array['extend_1'];

        return true;

    }

    /**
     * 订单结算
     * 根据收货地址查询配送方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @param int $send_region_id 选中的配送区域ID
     * @return array|null
     */
    public function checkoutSendType( &$bill_master_array, &$cust_buy_log, $send_region_id ) {

        $send_type_array = null;

        //sql语句
        $search_sql = 'SELECT ss.sendTypeID,ss.sendTypeName,ss.description,ssa.send_region_id,ssa.send_region_name,ss.ziti
                        FROM sys_send_area_list ssal
                        INNER JOIN sys_send_area ssa ON ssa.send_region_id = ssal.send_region_id
                        INNER JOIN sys_sendtype ss ON ss.sendTypeID = ssa.sendTypeID AND ss.useFlg = 1
                        WHERE ssal.regionID  = ? AND ssal.send_region_id = ?';

        //根据地区查询
        if (!empty($bill_master_array['countyid']))
            $send_type_array = $this->dbca->stmt_query($search_sql, 'ii', $bill_master_array['countyid'], $send_region_id);

        //根据市区查询
        if (!$send_type_array)
            $send_type_array = $this->dbca->stmt_query($search_sql, 'ii', $bill_master_array['cityid'], $send_region_id);

        //根据省份
        if (!$send_type_array)
            $send_type_array = $this->dbca->stmt_query($search_sql, 'ii', $bill_master_array['provinceid'], $send_region_id);


        if (!$send_type_array)
            return false;
        $item = $send_type_array[0];

        $bill_master_array['sendTypeID'] = $item->sendTypeID;
        $bill_master_array['sendTypeName'] = $item->sendTypeName;
        $bill_master_array['send_region_id'] = $item->send_region_id;
        $bill_master_array['send_region_name'] = $item->send_region_name;
        $bill_master_array['ziti'] = $item->ziti;
        $bill_master_array['extend_1'] = null;
        $bill_master_array['extend_9'] = null;
        $bill_master_array['extend_10'] = null;
        $bill_master_array['extend_2'] = null;
        $bill_master_array['extend_11'] = null;

        $cust_buy_log['send_region_id'] = $item->send_region_id;

        return true;


    }

    /**
     * 根据配送区域和商品总重量，计算运费
     * @param $send_area_id
     * @param $goods_weight
     * @return float|int|null
     */
    public function checkoutCalculateFreight($send_area_id, $goods_weight) {

        $send_area_obj = new entity('sys_send_area');
        $send_area_obj->send_region_id = $send_area_id;
        if (!$this->dbca->fetch($send_area_obj))
            return -1;
            
        if ($send_area_obj->ykg >= $goods_weight){
            return $send_area_obj->freight;
        } else {
            $overweight = ceil($goods_weight - $send_area_obj->ykg);
            return round($send_area_obj->freight + $overweight * $send_area_obj->overweight_freight, 2);
        }

    }


    /**
     * 订单结算
     * 根据配送方式，查询支付方式
     * @param string $source 订单数组
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return array|null
     */
    public function checkoutPayTypeList( $source, &$bill_master_array, &$cust_buy_log=null ) {

        //查询支付类型
        $arg_array = array(
            'sendTypeID' => $bill_master_array['sendTypeID'],
            'sortname' => 'pay_type',
            'sortorder' => 'ASC'
        );
        $pay_array = $this->searchPayTypeFromSendPay( $arg_array );
        if (!$pay_array)
            return null;

        foreach($pay_array as &$item) {

            //根据支付类型查询支付方式
            $arg_array = array (
                'source' => $source,
                'type' => $item['pay_type'],
                'sortname' => 'payTypeID',
                'sortorder' => 'ASC',
                'useFlg' => 1
            );

            $pay_type_array = $this->searchPayType( $arg_array );
            if (!$pay_type_array)
                continue;

            $item_array[] = array(
                'sendTypeID' => $item['sendTypeID'],
                'type' => $item['pay_type'],
                'list' => $pay_type_array
            );


            foreach($pay_type_array as &$pay_type) {
                if (!isset($bill_master_array['payTypeID']) || (!is_null($cust_buy_log) && $pay_type['payTypeID'] == $cust_buy_log['payTypeID'])) {
                    $bill_master_array['pay_type'] = $pay_type['type'];
                    $bill_master_array['payTypeID'] = $pay_type['payTypeID'];
                    $bill_master_array['payTypeName'] = $pay_type['payTypeName'];
                }
            }

        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['pay_type'] = $bill_master_array['pay_type'];
            $cust_buy_log['payTypeID'] = $bill_master_array['payTypeID'];
        }

        return $item_array;

    }


    /**
     * 订单结算
     * 查询支付方式详情
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param int $payTypeID 选中的支付方式ID
     * @return array|null
     */
    public function checkoutPayType( &$bill_master_array, &$cust_buy_log, $payTypeID ) {

        //根据支付类型查询支付方式
        $arg_array = array(
            'payTypeID' => $payTypeID
        );

        $pay_type_array = $this->searchPayType( $arg_array );
        if (!$pay_type_array)
            return false;

        $bill_master_array['pay_type'] = $pay_type_array[0]['type'];
        $bill_master_array['payTypeID'] = $pay_type_array[0]['payTypeID'];
        $bill_master_array['payTypeName'] = $pay_type_array[0]['payTypeName'];

        $cust_buy_log['pay_type'] = $bill_master_array['pay_type'];
        $cust_buy_log['payTypeID'] = $bill_master_array['payTypeID'];

        return true;

    }

    /**
     * 查询支付方式
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sendTypeID' => int 配送方式ID
     *      'useFlg' => int 使用标志
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchSendType( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //配送方式ID
        if (isset($arg_array['sendTypeID']) && is_numeric($arg_array['sendTypeID'])) {
            $sql .= ' AND sendTypeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sendTypeID'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }


        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_sendtype ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_sendtype ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'sendTypeID' => $item->sendTypeID,
                    'sendTypeName' => $item->sendTypeName,
                    'description' => $item->description,
                    'useFlg' => $item->useFlg,
                    'ziti' => $item->ziti
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 查询配送区域
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sendTypeID' => int 配送方式ID
     *      'send_region_id' => int 配送区域ID
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchSendArea( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //配送方式ID
        if (isset($arg_array['sendTypeID']) && is_numeric($arg_array['sendTypeID'])) {
            $sql .= ' AND sendTypeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sendTypeID'];
        }

        //使用标志
        if (isset($arg_array['send_region_id']) && is_numeric($arg_array['send_region_id'])) {
            $sql .= ' AND send_region_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['send_region_id'];
        }


        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_send_area ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_send_area ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'sendTypeID' => $item->sendTypeID,
                    'send_region_id' => $item->send_region_id,
                    'send_region_name' => $item->send_region_name,
                    'freight' => $item->freight,
                    'ykg' => $item->ykg,
                    'overweight_freight' => $item->overweight_freight,
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 查询配送区域详细地址
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
            'sendTypeID' => int 配送方式ID
            'send_region_id' => int 配送区域ID
     * )
     * @return null
     */
    public function searchSendAreaList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //配送方式ID
        if (isset($arg_array['sendTypeID']) && is_numeric($arg_array['sendTypeID'])) {
            $sql .= ' AND sendTypeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sendTypeID'];
        }

        //使用标志
        if (isset($arg_array['send_region_id']) && is_numeric($arg_array['send_region_id'])) {
            $sql .= ' AND send_region_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['send_region_id'];
        }


        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_send_area_list ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            //实例化公共类
            $commHandler = new commHandler( $this->dbca );

            foreach($temp_item_array as &$item) {

                $province_name = '';
                $city_name = '';
                $county_name = '';

                $r_arg_array = array(
                    'id' => $item->regionID
                );
                $result_array = $commHandler->searchRegion( $r_arg_array );
                if (!$result_array)
                    continue;
                if ($result_array[0]['region_level'] == 1) {

                    $province_name = $result_array[0]['name'];

                } else if ($result_array[0]['region_level'] == 2) {

                    $city_name = $result_array[0]['name'];

                    //查询省份名称
                    $r_arg_array = array(
                        'id' => $result_array[0]['parentID']
                    );

                    $province_array = $commHandler->searchRegion( $r_arg_array );
                    if (!$province_array)
                        continue;

                    $province_name = $province_array[0]['name'];

                } else {

                    $county_name = $result_array[0]['name'];

                    //查询市区名称
                    $r_arg_array = array(
                        'id' => $result_array[0]['parentID']
                    );
                    $city_array = $commHandler->searchRegion( $r_arg_array );
                    if (!$city_array)
                        continue;

                    $city_name = $city_array[0]['name'];

                    //查询省份名称
                    $r_arg_array = array(
                        'id' => $city_array[0]['parentID']
                    );

                    $province_array = $commHandler->searchRegion( $r_arg_array );
                    if (!$province_array)
                        continue;

                    $province_name = $province_array[0]['name'];


                }

                $item_array[] = array(
                    'sendTypeID' => $item->sendTypeID,
                    'send_region_id' => $item->send_region_id,
                    'regionID' => $item->regionID,
                    'levelFlg' => $item->levelFlg,
                    'province_name' => $province_name,
                    'city_name' => $city_name,
                    'county_name' => $county_name,
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 新增/修改配送区域
     * @param array $arg_array 参数数组
     * $arg_array = array (
            'sendTypeID' => int 配送方式ID
            'send_region_id' => int 配送区域ID
            'send_region_name' => string 配送区域名称
            'ykg' => int 首重
            'freight' => double 首重运费
            'overweight_freight' => double 超重运费
     *      'province_box' => array 身份数组  【可选】
     *      'city_box' => array 市区数组      【可选】
     *      'county_box' => array 地区数组    【可选】
     * )
     * @return array
     * @throws Exception
     */
    public function editSendArea ( $arg_array ) {

        //实例化公共类
        $commHandler = new commHandler( $this->dbca );

        if (!isset($arg_array['sendTypeID'], $arg_array['send_region_id'], $arg_array['send_region_name'], $arg_array['ykg'], $arg_array['freight'], $arg_array['overweight_freight']))
            return array( 'code' => 400, 'message' => '参数错误' );

        if (!$commHandler->isInteger($arg_array['sendTypeID'], 2) || !$commHandler->isInteger($arg_array['send_region_id'], 2))
            return array( 'code' => 400, 'message' => '参数错误' );

        if (empty($arg_array['send_region_name']))
            return array( 'code' => 400, 'message' => '配送区域名称不能为空' );

        if (!$commHandler->isInteger($arg_array['ykg'], 1))
            return array( 'code' => 400, 'message' => '首重必须是大于0的整数' );

        if (!$commHandler->isMoney($arg_array['freight'], 2))
            return array( 'code' => 400, 'message' => '首重运费必须是大于等于0的金额' );

        if (!$commHandler->isMoney($arg_array['overweight_freight'], 2))
            return array( 'code' => 400, 'message' => '超重运费必须是大于等于0的金额' );

        if (!isset($arg_array['province_box']) || !is_array($arg_array['province_box']))
            $arg_array['province_box'] = null;
        if (!isset($arg_array['city_box']) || !is_array($arg_array['city_box']))
            $arg_array['city_box'] = null;
        if (!isset($arg_array['county_box']) || !is_array($arg_array['county_box']))
            $arg_array['county_box'] = null;

        if (empty($arg_array['province_box']) && empty($arg_array['city_box']) && empty($arg_array['county_box']))
            return array( 'code' => 400, 'message' => '请选择配送区域' );

        try {



            //查询配送方式
            $send_obj = new entity('sys_sendtype');
            $send_obj->sendTypeID = $arg_array['sendTypeID'];
            if (!$this->dbca->fetch($send_obj))
                throw new Exception('配送方式信息没有找到', 404);

            //配送区域主表信息
            $send_area_obj = new entity('sys_send_area');
            $send_area_obj->sendTypeID = $arg_array['sendTypeID'];
            if ($arg_array['send_region_id'] > 0) {

                //查询配送区域
                $send_area_obj->send_region_id = $arg_array['send_region_id'];
                if (!$this->dbca->fetch($send_area_obj))
                    throw new Exception('配送区域信息没有找到', 404);
                $send_area_obj->editor = $arg_array['editor'];

            } else {
                $send_area_obj->creator = $arg_array['editor'];
            }
            $send_area_obj->send_region_name = $commHandler->convertVar($arg_array['send_region_name']);
            $send_area_obj->freight = $arg_array['freight'];
            $send_area_obj->ykg = $arg_array['ykg'];
            $send_area_obj->overweight_freight = $arg_array['overweight_freight'];


            $send_area_list_array = array();

            //省份
            if (!empty($arg_array['province_box'])) {

                foreach($arg_array['province_box'] as &$p_id) {

                    $r_arg_array = array(
                        'id' => $p_id,
                        'level' => 1
                    );
                    if (!$commHandler->searchRegion( $r_arg_array ))
                        continue;

                    $send_area_list_obj = new entity('sys_send_area_list');
                    $send_area_list_obj->creator = $arg_array['editor'];
                    $send_area_list_obj->sendTypeID = $arg_array['sendTypeID'];
                    $send_area_list_obj->regionID = $p_id;
                    $send_area_list_obj->levelFlg = 1;
                    $send_area_list_array[] = $send_area_list_obj;

                }

            }

            //市区
            if (!empty($arg_array['city_box'])) {

                foreach($arg_array['city_box'] as &$c_id) {

                    $r_arg_array = array(
                        'id' => $c_id,
                        'level' => 2
                    );
                    if (!$commHandler->searchRegion( $r_arg_array ))
                        continue;

                    $send_area_list_obj = new entity('sys_send_area_list');
                    $send_area_list_obj->creator = $arg_array['editor'];
                    $send_area_list_obj->sendTypeID = $arg_array['sendTypeID'];
                    $send_area_list_obj->regionID = $c_id;
                    $send_area_list_obj->levelFlg = 2;
                    $send_area_list_array[] = $send_area_list_obj;

                }

            }

            //地区
            if (!empty($arg_array['county_box'])) {

                foreach($arg_array['county_box'] as &$cc_id) {

                    $r_arg_array = array(
                        'id' => $cc_id,
                        'level' => 3
                    );
                    if (!$commHandler->searchRegion( $r_arg_array ))
                        continue;

                    $send_area_list_obj = new entity('sys_send_area_list');
                    $send_area_list_obj->creator = $arg_array['editor'];
                    $send_area_list_obj->sendTypeID = $arg_array['sendTypeID'];
                    $send_area_list_obj->regionID = $cc_id;
                    $send_area_list_obj->levelFlg = 3;
                    $send_area_list_array[] = $send_area_list_obj;

                }

            }

            if (empty($send_area_list_array))
                throw new Exception('请选择配送区域', 404);

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误，请与系统管理员联系' );
            }

        }


        try {

            //开始事务
            $this->dbca->beginTransaction();

            //保存/更新配送区域主表信息
            if (!$this->dbca->save($send_area_obj))
                throw new Exception('系统错误，请与系统管理员联系', 500);

            $send_region_id = $arg_array['send_region_id'];
            if ($send_region_id == 0)
                $send_region_id = $this->dbca->insert_id;
            else {

                //删除配送区域具体的区域信息
                $sql = 'DELETE FROM sys_send_area_list WHERE send_region_id = ?';
                if (!$this->dbca->stmt_query($sql, 'i', $send_region_id))
                    throw new Exception('系统错误，请与系统管理员联系', 500);

            }


            //保存配送区域对应地址
            foreach($send_area_list_array as &$list_obj) {

                $list_obj->send_region_id = $send_region_id;
                if (!$this->dbca->save($list_obj))
                    throw new Exception('系统错误，请与系统管理员联系', 500);

            }

            //提交事务
            $this->dbca->commit();

        } catch (Exception $e) {

            //回滚事务
            $this->dbca->rollBack();

            if ($e->getCode() == 0)
                error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误，请与系统管理员联系' );

        }

        if ($arg_array['send_region_id'] == 0)
            return array( 'code' => 200, 'message' => '配送区域保存成功' );
        else
            return array( 'code' => 200, 'message' => '配送区域修改成功' );
		
	}


    /**
     * 新增或修改配送方式
     * @param array $arg_array 参数数组
     * $arg_array = array(
            'sendTypeID' => int 配送方式ID
            'sendTypeName' => string 配送方式名称
            'description' => string 配送方式备注
            'useFlg' => int 使用标志
            'pay_type' => array 支付类型数组
     * )
     * @return array
     * @throws Exception
     */
    public function editSendType ( $arg_array ) {

        if (!isset($arg_array['sendTypeID'], $arg_array['sendTypeName'], $arg_array['description'], $arg_array['useFlg'], $arg_array['pay_type']))
            return array( 'code' => 400, 'message' => '参数错误' );

        if (!is_numeric($arg_array['sendTypeID']) || $arg_array['sendTypeID'] < 0)
            return array( 'code' => 400, 'message' => '参数错误' );

        if (empty($arg_array['sendTypeName']))
            return array( 'code' => 400, 'message' => '配送方式名称不能为空' );

        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择使用标志' );

        if (empty($arg_array['pay_type']) || !is_array($arg_array['pay_type']))
            return array( 'code' => 400, 'message' => '请选择支付方式类型' );

        //实例化公共类
        $commHandler = new commHandler();

		try {

            //开始事务
            $this->dbca->beginTransaction();

            //保存配送方式
            $send_type_obj = new entity('sys_sendtype');
            if ($arg_array['sendTypeID'] > 0) {

                //查询配送方式
                $send_type_obj->sendTypeID = $arg_array['sendTypeID'];
                if (!$this->dbca->fetch($send_type_obj))
                    throw new Exception('配送方式信息没有找到', 400);
                $send_type_obj->editor = $arg_array['editor'];

                //删除配送方式对应的支付类型数据
                $sql = 'DELETE FROM sys_sendtype_pay WHERE sendTypeID = ?';
                $this->dbca->stmt_query($sql, 'i', $arg_array['sendTypeID']);

            } else {
                $send_type_obj->creator = $arg_array['editor'];
            }
            $send_type_obj->sendTypeName = $commHandler->convertVar($arg_array['sendTypeName']);
            $send_type_obj->description = $commHandler->convertVar($arg_array['description']);
            $send_type_obj->useFlg = $arg_array['useFlg'];
            $send_type_obj->ziti = $arg_array['ziti'];
            if (!$this->dbca->save($send_type_obj))
                throw new Exception('系统错误，请与系统管理员联系', 500);


            $sendTypeID = $arg_array['sendTypeID'];

            if ($sendTypeID == 0)
                $sendTypeID = $this->dbca->insert_id;


            foreach($arg_array['pay_type'] as &$pay_type) {

                if (!in_array($pay_type, array(1, 2, 3)))
                    throw new Exception('支付类型错误', 400);

                //保存配送方式支付类型
                $pay_type_obj = new entity('sys_sendtype_pay');
                $pay_type_obj->creator = $arg_array['editor'];
                $pay_type_obj->sendTypeID = $sendTypeID;
                $pay_type_obj->pay_type = $pay_type;
                if (!$this->dbca->save($pay_type_obj))
                    throw new Exception('系统错误，请与系统管理员联系', 500);

            }

            //提交事务
            $this->dbca->commit();

		} catch (Exception $e) {

            //回滚事务
            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误，请与系统管理员联系' );
            }

		}

        if ($arg_array['sendTypeID'] == 0)
            return array( 'code' => 200, 'message' => '配送方式保存成功' );
        else
            return array( 'code' => 200, 'message' => '配送方式修改成功' );
	
	}

    /**
     * 删除配送区域
     * @param array $arg_array 配送区域ID数组
     * $arg_array = array(
            0 => int, 区域ID
            1 => int, 区域ID
            ...
     * )
     * @param bool $isTransaction 是否使用事务
     * @return array
     * @throws Exception
     */
    public function deleteSendArea ( $arg_array, $isTransaction=true ) {

        if (empty($arg_array) || !is_array($arg_array))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //开始事务
            if ($isTransaction)
                $this->dbca->beginTransaction();

            foreach($arg_array as &$id) {

                if (!is_numeric($id))
                    continue;

                //删除配送区域
                $send_area_obj = new entity('sys_send_area');
                $send_area_obj->send_region_id = $id;
                if (!$this->dbca->fetch($send_area_obj))
                    continue;
                if (!$this->dbca->delete($send_area_obj))
                    throw new Exception('系统错误，请与系统管理员联系', 500);


                //删除配送区域具体的区域信息
                $sql = 'DELETE FROM sys_send_area_list WHERE send_region_id = ?';
                if (!$this->dbca->stmt_query($sql, 'i', $id))
                    throw new Exception('系统错误，请与系统管理员联系', 500);

            }

            //提交事务
            if ($isTransaction)
                $this->dbca->commit();

        } catch (Exception $e) {

            //回滚事务
            if ($isTransaction)
                $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误，请与系统管理员联系' );
            }

        }

        return array( 'code' => 200, 'message' => '配送区域删除成功' );

	}


    /**
     * 删除配送方式
     * @param array $arg_array 配送方式ID数组
     * $arg_array = array(
            0 => int, 配送方式ID
            1 => int, 配送方式ID
            ...
     * )
     * @return array
     * @throws Exception
     */
	public function deleteSendType ( $arg_array ) {

        if (empty($arg_array) || !is_array($arg_array))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //开始事务
            $this->dbca->beginTransaction();

            foreach($arg_array as &$id) {

                if (!is_numeric($id))
                    continue;

                //删除配送方式
                $send_type_obj = new entity('sys_sendtype');
                $send_type_obj->sendTypeID = $id;
                if (!$this->dbca->fetch($send_type_obj))
                    continue;
                if (!$this->dbca->delete($send_type_obj))
                    throw new Exception('系统错误，请与系统管理员联系1', 500);

                //检查配送方式下是否已配置区域
                $area_arg_array = array(
                    'sendTypeID' => $id
                );
                $area_array = $this->searchSendArea( $area_arg_array );
                if ($area_array) {

                    //生成要删除配送区域ID数组
                    $del_arg_array = array();
                    foreach($area_array as &$area) {
                        $del_arg_array[] = $area['send_region_id'];
                    }

                    //删除配送区域
                    $result_array = $this->deleteSendArea($del_arg_array, false);
                    if ($result_array['code'] != 200)
                        throw new Exception($result_array['message'], $result_array['code']);

                }


            }

            //提交事务
            $this->dbca->commit();

        } catch (Exception $e) {

            //回滚事务
            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误，请与系统管理员联系3' );
            }

        }

        return array( 'code' => 200, 'message' => '配送方式删除成功' );

	}

    /**
     * 配送方式启用/禁用
     * @param $arg_array
     * $arg_array = array(
            sendTypeID => int 配送方式ID【必选】
            flg => int 配送方式状态【必选】
     * )
     * @return array
     */
    public function changeSendTypeUseflg ( $arg_array ) {

        if (!isset($arg_array['sendTypeID'], $arg_array['flg']) || !is_numeric($arg_array['sendTypeID']) || !in_array($arg_array['flg'], array(0,1)))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //查询配送方式
            $sendType_obj = new entity('sys_sendtype');
            $sendType_obj->sendTypeID = $arg_array['sendTypeID'];
            if (!$this->dbca->fetch($sendType_obj))
                return array( 'code' => 404, 'message' => '配送方式信息没有找到' );

            if ($arg_array['flg'] == 0 && $sendType_obj->useFlg == 0)
                return array( 'code' => 406, 'message' => '配送方式已禁用' );

            if ($arg_array['flg'] == 1 && $sendType_obj->useFlg == 1)
                return array( 'code' => 406, 'message' => '配送方式已启用' );

            $sendType_obj->useFlg = $arg_array['flg'];
            $this->dbca->save($sendType_obj);

            if ($arg_array['flg'])
                return array( 'code' => 200, 'message' => '启用成功' );
            else
                return array( 'code' => 200, 'message' => '禁用成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

	}


    /**
     * 新增或修改支付方式
     * @param array $arg_array 支付方式参数
     * @return array
     */
    public function editPayType ( $arg_array ) {

        quanli_include('module/base/fileDirHandler.class.php');

        if (!isset($arg_array['payTypeName'], $arg_array['type'],  $arg_array['operator'], $arg_array['b2c_usable'], $arg_array['wap_usable'], $arg_array['wx_usable'], $arg_array['app_usable']) || empty($arg_array['operator'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        //支付方式名称
		if (empty($arg_array['payTypeName'])) {
            return array( 'code' => 400, 'message' => '支付方式名称不能为空' );
        }

        //支付方式类型
        if (!in_array($arg_array['type'], array(1, 2, 3, 4, 5))) {
            return array( 'code' => 400, 'message' => '请选择支付方式类型' );
        }

        //平台类型
        if ($arg_array['type'] == 2) {
            if (!isset($arg_array['payment_platform']) || !in_array($arg_array['payment_platform'], array(1, 2))) {
                return array( 'code' => 400, 'message' => '请选择平台类型' );
            }
        } else {
            $arg_array['payment_platform'] = null;
        }

        //b2c是否可用
        if (!in_array($arg_array['b2c_usable'], array(0, 1))) {
            $arg_array['b2c_usable'] = 1;
        }

        //wap是否可用
        if (!in_array($arg_array['wap_usable'], array(0, 1))) {
            $arg_array['wap_usable'] = 1;
        }

        //wx是否可用
        if (!in_array($arg_array['wx_usable'], array(0, 1))) {
            $arg_array['wx_usable'] = 1;
        }

        //app是否可用
        if (!in_array($arg_array['app_usable'], array(0, 1))) {
            $arg_array['app_usable'] = 1;
        }

        //支付方式ID
        if (!isset($arg_array['payTypeID']) || !is_numeric($arg_array['payTypeID']) ||$arg_array['payTypeID'] <= 0) {
            $arg_array['payTypeID'] = 0;
        }

        //支付方式logo
        if (!isset($arg_array['logo_img']) || empty($arg_array['logo_img'])) {
            $arg_array['logo_img'] = null;
        }

        $fileDirHandler = new fileDirHandler();

        if (empty($arg_array['logo_img'])) {
            $oldName = null;
        } else {
            $oldName = G_APPLICATION_BASEPATH . $arg_array['logo_img'];
            $newName = G_IMAGE_SAVE_ROOT . 'system/' . $fileDirHandler->basename($oldName);
        }

        try {

            $pay_type_obj = new entity('sys_paytype');
            if ($arg_array['payTypeID'] > 0) {

                $pay_type_obj->payTypeID = $arg_array['payTypeID'];
                if (!$this->dbca->fetch($pay_type_obj)) {
                    throw new Exception('支付方式信息没有找到', 404);
                }

            } else {

                $pay_type_obj->creator = $arg_array['operator'];
                $pay_type_obj->useFlg = 1;

            }
            if (!empty($oldName)) {
                if (!$fileDirHandler->move($oldName, $newName)) {
                    throw new Exception('文件移动失败', 404);
                }
                $pay_type_obj->logo_img = 'system/' . $fileDirHandler->basename($oldName);
            }
            $pay_type_obj->payTypeName = $arg_array['payTypeName'];
            $pay_type_obj->memo = $arg_array['memo'];
            $pay_type_obj->type = $arg_array['type'];
            $pay_type_obj->payment_platform = $arg_array['payment_platform'];
            $pay_type_obj->b2c_usable = $arg_array['b2c_usable'];
            $pay_type_obj->wap_usable = $arg_array['wap_usable'];
            $pay_type_obj->wx_usable = $arg_array['wx_usable'];
            $pay_type_obj->app_usable = $arg_array['app_usable'];
            $pay_type_obj->is_default = 0;
            $this->dbca->save($pay_type_obj);

            return array( 'code' => 200, 'message' => '支付方式保存成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

	
	}

    /**
     * 新增、编辑付款方式
     * @param array $arg_array 参数数组
     * @return array
     */
    public function editPayWay ( $arg_array ) {

        if (!isset($arg_array['payTypeID'], $arg_array['operator'], $arg_array['name'], $arg_array['useFlg'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        //实例公共类
        $commHandler = new commHandler();

        if (!$commHandler->isInteger( $arg_array['payTypeID'], 1 )) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (empty($arg_array['name'])) {
            return array( 'code' => 400, 'message' => '付款方式名称不能为空' );
        }

        if (!in_array($arg_array['useFlg'], array(0, 1))) {
            $arg_array['useFlg'] = 1;
        }

        if (!isset($arg_array['id']) || !$commHandler->isInteger( $arg_array['id'], 1 )) {
            $arg_array['id'] = 0;
        }

        try {

            //查询支付方式
            $p_arg_array = array (
                'payTypeID' => $arg_array['payTypeID']
            );
            $pay_type_array = $this->searchPayType( $p_arg_array );
            if (!$pay_type_array) {
                throw new Exception('支付方式信息没有找到', 404);
            }
            if (in_array($pay_type_array[0]['type'], array(2, 3, 4, 5))) {
                throw new Exception('选择的支付方式类型不是货到付款', 404);
            }

            //检查付款方式
            $method_obj = new entity('sys_payment_method');
            $method_obj->payTypeID = $arg_array['payTypeID'];
            if ($arg_array['id'] > 0) {
                $method_obj->id = $arg_array['id'];
                if (!$this->dbca->fetch($method_obj)) {
                    throw new Exception('付款方式信息没有找到', 404);
                }
            } else {
                $method_obj->creator = $arg_array['operator'];
            }
            $method_obj->name = $commHandler->convertVar( $arg_array['name'] );
            $method_obj->useFlg = $arg_array['useFlg'];
            $this->dbca->save($method_obj);

            return array( 'code' => 200, 'message' => '付款方式保存成功' );

        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }
		
	}

    /**
     * 新增/修改银行信息
     * @param array $arg_array 参数数组
     * @return array
     */
    public function editBank ( $arg_array ) {

        quanli_include('module/base/fileDirHandler.class.php');

        if (!isset($arg_array['payTypeID'], $arg_array['bank_id'], $arg_array['bank_name'], $arg_array['payee'], $arg_array['bank_account_numbers'],
            $arg_array['bank_location'], $arg_array['bank_branch'], $arg_array['useFlg'], $arg_array['operator'])
            || empty($arg_array['operator']) || !is_numeric($arg_array['payTypeID'])) {

            return array( 'code' => 400, 'message' => '参数错误' );

        }

        //银行名称
        if (empty($arg_array['bank_name'])) {
            return array( 'code' => 400, 'message' => '银行名称不能为空' );
        }

        //收款人
        if (empty($arg_array['payee'])) {
            return array( 'code' => 400, 'message' => '收款人不能为空' );
        }

        //收款帐号
        if (empty($arg_array['bank_account_numbers'])) {
            return array( 'code' => 400, 'message' => '收款帐号不能为空' );
        }

        //银行所在地
        if (empty($arg_array['bank_location'])) {
            return array( 'code' => 400, 'message' => '银行所在地不能为空' );
        }

        //银行网点
        if (empty($arg_array['bank_branch'])) {
            return array( 'code' => 400, 'message' => '银行网点不能为空' );
        }

        //logo
        if (!isset($arg_array['bank_logo']) || empty($arg_array['bank_logo'])) {
            $arg_array['bank_logo'] = null;
        }

        $fileDirHandler = new fileDirHandler();

        if (empty($arg_array['bank_logo'])) {
            $oldName = null;
        } else {
            $oldName = G_APPLICATION_BASEPATH . $arg_array['bank_logo'];
            $newName = G_IMAGE_SAVE_ROOT . 'system/' . $fileDirHandler->basename($oldName);
        }

        //银行ID
        if (!isset($arg_array['bank_id']) || empty($arg_array['bank_id'])) {
            $arg_array['bank_id'] = 0;
        }

        try {

            $bank_obj = new entity('sys_bank');
            $bank_obj->payTypeID = $arg_array['payTypeID'];
            if ($arg_array['bank_id'] > 0) {

                $bank_obj->bank_id = $arg_array['bank_id'];
                if (!$this->dbca->fetch($bank_obj)) {
                    throw new Exception('银行信息没有找到', 404);
                }

            } else {

                $bank_obj->creator = $arg_array['operator'];

            }
            if (!empty($oldName)) {
                if (!$fileDirHandler->move($oldName, $newName)) {
                    throw new Exception('文件移动失败', 404);
                }
                $bank_obj->bank_logo = 'system/' . $fileDirHandler->basename($oldName);
            }
            $bank_obj->bank_name = $arg_array['bank_name'];
            $bank_obj->payee = $arg_array['payee'];
            $bank_obj->bank_account_numbers = $arg_array['bank_account_numbers'];
            $bank_obj->bank_location = $arg_array['bank_location'];
            $bank_obj->bank_branch = $arg_array['bank_branch'];
            $bank_obj->useFlg = $arg_array['useFlg'];
            $this->dbca->save( $bank_obj );

            return array( 'code' => 200, 'message' => '银行信息保存成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }
	
	}

    /**
     * 保存在线支付参数
     * @param array $arg_array 参数数组
     * @return array
     */
    public function editOnline ( $arg_array  ){

        if (!isset($arg_array['payTypeID'], $arg_array['operator'], $arg_array['partnerID'], $arg_array['alias'], $arg_array['sign_type'])
            || !is_numeric($arg_array['payTypeID']) || empty($arg_array['operator'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (empty($arg_array['partnerID'])) {
            return array( 'code' => 400, 'message' => '商户号不能为空' );
        }
        if (empty($arg_array['alias'])) {
            return array( 'code' => 400, 'message' => '别名不能为空' );
        }
        if (empty($arg_array['sign_type'])) {
            return array( 'code' => 400, 'message' => '签名方式不能为空' );
        }

        try {

            $p_arg_array = array(
                'payTypeID' => $arg_array['payTypeID']
            );
            $pay_type_array = $this->searchPayType( $p_arg_array );
            if (!$pay_type_array) {
                throw new Exception('支付方式信息没有找到', 404);
            }
            if (in_array($pay_type_array[0]['type'], array(1, 3, 4, 5))) {
                throw new Exception('选择的支付方式类型不是在线支付', 404);
            }

            $online_obj = new entity('sys_online_parameter');
            $online_obj->payTypeID = $arg_array['payTypeID'];
            if (!$this->dbca->fetch($online_obj)) {
                $online_obj->creator = $arg_array['operator'];
            } else {
                $online_obj->editor = $arg_array['operator'];
            }
            $online_obj->payTypeName = $pay_type_array[0]['payTypeName'];
            $online_obj->partnerID = $arg_array['partnerID'];
            $online_obj->seller_email = $arg_array['seller_email'];
            $online_obj->seller_name = $arg_array['seller_name'];
            $online_obj->service = $arg_array['service'];
            $online_obj->md5_key = $arg_array['md5_key'];
            $online_obj->private_key = $arg_array['private_key'];
            $online_obj->public_key = $arg_array['public_key'];
            $online_obj->public_key = $arg_array['public_key'];
            $online_obj->sign_type = $arg_array['sign_type'];
            $online_obj->charset = $arg_array['charset'];
            $online_obj->gate_url = $arg_array['gate_url'];
            $online_obj->version = $arg_array['version'];
            $online_obj->extend_1 = $arg_array['extend_1'];
            $online_obj->extend_2 = $arg_array['extend_2'];
            $online_obj->extend_3 = $arg_array['extend_3'];
            $online_obj->extend_4 = $arg_array['extend_4'];
            $online_obj->extend_5 = $arg_array['extend_5'];
            $online_obj->alias = $arg_array['alias'];
            $this->dbca->save($online_obj);

            return array( 'code' => 200, 'message' => '保存成功' );

        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }
	
	}

    /**
     * 删除货到付款 -- 付款方式
     * @param array $pay_array 付款方式ID数组
     * @return array
     * @throws Exception
     */
    public function deletePayWay ( $pay_array ){

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            foreach($pay_array as &$id) {

                //查询付款方式
                $payment_method_obj = new entity('sys_payment_method');
                $payment_method_obj->id = $id;
                if (!$this->dbca->fetch($payment_method_obj)) {
                    throw new Exception('付款方式信息没有找到', 404);
                }

                //删除付款方式信息
                $this->dbca->delete( $payment_method_obj );

            }

            return array( 'code' => 200, 'message' => '付款方式删除成功' );

        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

	}


    /**
     * 删除银行
     * @param array $bank_array 银行ID数组
     * @return array
     * @throws Exception
     */
    public function deleteBank ( $bank_array ){

        if (!isset($bank_array) || !is_array($bank_array) || empty($bank_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            foreach($bank_array as &$bank_id) {

                //查询银行信息
                $bank_obj = new entity('sys_bank');
                $bank_obj->bank_id = $bank_id;
                if (!$this->dbca->fetch($bank_obj)) {
                    throw new Exception('银行信息没有找到', 404);
                }

                //删除银行信息
                $this->dbca->delete( $bank_obj );

            }

            return array( 'code' => 200, 'message' => '银行删除成功' );

        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 付款方式启用/禁用
     * @param array $arg_array 参数数组
     * @return array
     */
    public function changePayWayUseFlg( $arg_array ) {

        if (!isset($arg_array['id'], $arg_array['payTypeID']) || !is_numeric($arg_array['id']) || !is_numeric($arg_array['payTypeID'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            //查询付款方式
            $payment_method_obj = new entity('sys_payment_method');
            $payment_method_obj->id = $arg_array['id'];
            $payment_method_obj->payTypeID = $arg_array['payTypeID'];
            if (!$this->dbca->fetch($payment_method_obj)) {
                throw new Exception('付款方式信息没有找到', 404);
            }

            //修改付款方式使用状态
            $useFlg = $payment_method_obj->useFlg;
            if ($useFlg) {
                $payment_method_obj->useFlg = 0;
            } else {
                $payment_method_obj->useFlg = 1;
            }
            $this->dbca->save($payment_method_obj);

            if ($useFlg) {
                return array( 'code' => 200, 'message' => '付款方式禁用成功' );
            } else {
                return array( 'code' => 200, 'message' => '付款方式启用成功' );
            }


        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


    /**
     * 银行启用/禁用
     * @param array $arg_array 参数数组
     * @return array
     */
    public function changeBankUse( $arg_array ) {

        if (!isset($arg_array['bank_id'], $arg_array['payTypeID']) || !is_numeric($arg_array['bank_id']) || !is_numeric($arg_array['payTypeID'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            //查询银行信息
            $bank_obj = new entity('sys_bank');
            $bank_obj->bank_id = $arg_array['bank_id'];
            $bank_obj->payTypeID = $arg_array['payTypeID'];
            if (!$this->dbca->fetch($bank_obj)) {
                throw new Exception('银行信息没有找到', 404);
            }

            //修改银行使用状态
            $useFlg = $bank_obj->useFlg;
            if ($useFlg) {
                $bank_obj->useFlg = 0;
            } else {
                $bank_obj->useFlg = 1;
            }
            $this->dbca->save($bank_obj);

            if ($useFlg) {
                return array( 'code' => 200, 'message' => '银行禁用成功' );
            } else {
                return array( 'code' => 200, 'message' => '银行启用成功' );
            }


        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 删除支付方式
     * @param array $pay_array 支付方式ID数组
     * @return array
     */
    public function deletePayType ( $pay_array ) {

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $this->dbca->beginTransaction();

            foreach($pay_array as &$payTypeID) {

                if (in_array($payTypeID, array(1, 2, 3))) {
                    throw new Exception('ID[1,2,3]是系统默认的支付方式<br />不可以删除', 404);
                }

                //查询支付方式
                $pay_obj = new entity('sys_paytype');
                $pay_obj->payTypeID = $payTypeID;
                if (!$this->dbca->fetch($pay_obj)) {
                    throw new Exception('支付方式信息没有找到', 404);
                }

                //判断支付方式类型，删除相关的数据
                if ($pay_obj->type == 1) { //货到付款类型，则删除支付类型

                    $delete_sql = 'DELETE FROM sys_payment_method WHERE payTypeID = ?';
                    $this->dbca->stmt_query($delete_sql, 'i', $payTypeID);

                } else if ($pay_obj->type == 2) { //在线支付类型，则删除支付参数表数据

                    $delete_sql = 'DELETE FROM sys_online_parameter WHERE payTypeID = ?';
                    $this->dbca->stmt_query($delete_sql, 'i', $payTypeID);

                }  else if ($pay_obj->type == 3) { //汇款类型，则删除汇款银行信息表数组

                    $delete_sql = 'DELETE FROM sys_bank WHERE payTypeID = ?';
                    $this->dbca->stmt_query($delete_sql, 'i', $payTypeID);

                }

                //删除支付方式信息
                $this->dbca->delete($pay_obj);

            }

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => '支付方式删除成功' );

        } catch ( Exception $e ) {

            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

	}


    /**
     * 支付方式启用/禁用
     * @param array $arg_array 参数数组
     * @return array
     */
    public function changePayType( $arg_array ) {

        if (!isset($arg_array['payTypeID']) || !is_numeric($arg_array['payTypeID'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            //查询支付方式
            $pay_obj = new entity('sys_paytype');
            $pay_obj->payTypeID = $arg_array['payTypeID'];
            if (!$this->dbca->fetch($pay_obj)) {
                throw new Exception('支付方式信息没有找到', 404);
            }
            if (in_array($arg_array['payTypeID'], array(1, 2, 3))) {
                throw new Exception('ID[1,2,3]是系统默认的支付方式<br />不可以禁用', 404);
            }

            //修改付款方式使用状态
            $useFlg = $pay_obj->useFlg;
            if ($useFlg) {
                $pay_obj->useFlg = 0;
            } else {
                $pay_obj->useFlg = 1;
            }
            $this->dbca->save($pay_obj);

            if ($useFlg) {
                return array( 'code' => 200, 'message' => '支付方式禁用成功' );
            } else {
                return array( 'code' => 200, 'message' => '支付方式启用成功' );
            }


        } catch ( Exception $e ) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


    /**
     * 查询SEO信息
     * $arg_array = array (
            'pageType' => int  页面类型  1.首页 2.列表页 3.详情页
            'sortname' => string 排序字段
            'sortorder' => string 排序方式
            'page' => int 分页页码
            'rp' => int 分页页数
     * )
     */
    public function searchSeoInfo( &$arg_array ){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['uid']) && is_numeric($arg_array['uid']) && $arg_array['uid'] > 0 ) {
            $sql .= ' AND uid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uid'];
        }

        if (isset($arg_array['pageType']) && in_array($arg_array['pageType'] , array(1,2,3)) ) {
            $sql .= ' AND pageType = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pageType'];
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'] , array(0, 1)) ) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_seo ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_seo ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'createTime' => $item->createTime,
                    'pageName' => $item->pageName,
                    'uid' => $item->uid,
                    'pageType' => $item->pageType,
                    'title' => $item->title,
                    'keywords' => $item->keywords,
                    'description' => $item->description,
                    'useFlg' => $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;


        }

    }

    /**
     * 保存SEO信息
     * @param $arg_array  参数数组
     * @return array
     */
    public function saveSeoInfo( $arg_array ){
		//商品详情页对应的商品的二级商品编号
    	$product_code = $arg_array['product_code'];
    	
        /**
         * uid参数说明：首页值为0，若是列表页，则是商品分类ID组合成的字符串，例如，大分类:10  中分类:110  小分类:10  则uid为1011010
         * 			    若是详情页，则是二级商品的product_code
         */
    	$uid         = $arg_array['uid'];

        /**
         * $pageType参数说明：页面类型：1.首页 2.列表页 3.详情页
         */
        $pageType    = $arg_array['pageType'];

        $pageName    = htmlspecialchars(trim($arg_array['pageName']));  //页面名称
        $title       = $arg_array['title'];                             //标题
        $keywords    = $arg_array['keywords'];                          //关键词
        $description = $arg_array['description'];                       //描述
        $useFlg      = $arg_array['useFlg'];                            //使用状态

        if ($pageType == 3 && $uid != $product_code && !empty($product_code)){
        	$goodsHandler = new goodsHandler($this->dbca);
        	$arg_arr = array('product_code'=>$product_code);
        	$result = $goodsHandler->searchGoodsList($arg_arr);
        	
        	if (!$result){
        		return array('code'=>400, 'message'=>'没有对应的商品');
        	}
        }
        
        /**
         * $uidString参数说明：当页面类型是[列表页]时，用于连接商品分类ID，将ID组织成字符串形成uid
         */
        $uidString = '';

        if( empty($pageName) )
            return array('code'=>400 , 'message'=>'页面名称不能为空');

        if( empty($pageType) )
            return array('code'=>400 , 'message'=>'请选择页面类型');

        //参数处理
        if( !empty($title) )
            $title = htmlspecialchars(trim($title));

        if( !empty($keywords) )
            $keywords = htmlspecialchars(trim($keywords));

        if( !empty($description) )
            $description = htmlspecialchars(trim($description));

        //如果页面类型是[列表页]，则要校验商品分类参数
        if( $pageType == 2 ){

            if( empty($arg_array['bigCate']) )
                return array('code'=>400 , 'message'=>'请选择大分类');

            $uidString .= $arg_array['bigCate'];  //大分类ID

            if( !empty($arg_array['midCate']) )
                $uidString .= $arg_array['midCate'];  //中分类ID

            if( !empty($arg_array['smallCate']) )
                $uidString .= $arg_array['smallCate'];  //小分类ID

        }

        $seo_obj = new entity('sys_seo');

        if( (is_numeric($uid) && $uid == 0) || !empty($uid)){

            $seo_obj->uid = $uid;
            $seo_obj->pageType = $pageType;

            if( !$this->dbca->fetch($seo_obj) )
                return array('message'=>'没有找到当前信息' , 'code'=>404 );

            //如果编辑时，改变了分类类型，则需要对改变后的分类类型进行查询，检查是否存在，若存在则给出提示
            if( $uid != $uidString && $pageType == 2 ){

                $check_exist  = 'SELECT * FROM sys_seo WHERE pageType=? AND uid=? ';
                $check_result = $this->dbca->stmt_query($check_exist,'ii',array($pageType,$uidString) );

                if( $check_result ){
                    return array('message'=>'该类型已存在' , 'code'=>400 );
                }else{
                    $seo_obj->uid = $uidString;
                }

            }
            
            if( $uid != $product_code && $pageType == 3 ){
            
            	$check_exist  = 'SELECT * FROM sys_seo WHERE pageType=? AND uid=? ';
            	$check_result = $this->dbca->stmt_query($check_exist,'ii',array($pageType,$product_code) );
            
            	if( $check_result ){
            		return array('message'=>'该类型已存在' , 'code'=>400 );
            	}else{
            		$seo_obj->uid = empty($product_code) ? 0 : $product_code;
            	}
            
            }

        }else{

            if( $pageType == 1 ){
                $seo_obj->uid = 0;
                $check_exist  = 'SELECT * FROM sys_seo WHERE pageType=?';
                $check_result = $this->dbca->stmt_query($check_exist,'i',$pageType);
            }elseif ($pageType == 2){
                $seo_obj->uid = $uidString;
                $check_exist  = 'SELECT * FROM sys_seo WHERE pageType=? AND uid=? ';
                $check_result = $this->dbca->stmt_query($check_exist,'ii',array($pageType,$uidString) );
            }elseif ($pageType == 3){
            	$seo_obj->uid = empty($product_code) ? 0 : $product_code;
            	$check_exist  = 'SELECT * FROM sys_seo WHERE pageType=? AND uid=? ';
            	$check_result = $this->dbca->stmt_query($check_exist,'ii',array($pageType,$product_code) );
            }
            $seo_obj->pageType = $pageType;
			
            if( $check_result )
                return array('message'=>'所选类型已存在' , 'code'=>400 );

            $seo_obj->creator = $arg_array['creator'];

        }

        $seo_obj->useFlg       = $useFlg;
        $seo_obj->pageName     = $pageName;
        $seo_obj->title        = $title;
        $seo_obj->keywords     = $keywords;
        $seo_obj->description  = $description;

        try{

            $this->dbca->save( $seo_obj );

            if( is_numeric($uid) ){
                return array('message'=>'更新成功' , 'code'=>200 );
            }else{
                return array('message'=>'保存成功' , 'code'=>200 );
            }

        }catch(Exception $e){

            error_log( $e->getMessage() );
            return array('message'=>'系统错误，请与管理员联系' , 'code'=>500 );

        }

    }

    /**
     * @param $arg_array  由uid,pageType组成的数组集合,例如：
     * $arg_array = array(
     *              [0]=>array(0,1),
     *              [1]=>array(1011010,2)
     *              ......
     *              [n]=>array(uid,pageType)
     * )
     */
    public function delSeoInfo( $arg_array ){

        //统计不存在的信息的数目
        $errorNum = 0;

        //记录要删除的entity对象
        $deleteObjArr = array();

        if( $arg_array ){

            //检查要删除的对象是否全部存在
            foreach( $arg_array as $value ){

                //检查信息是否存在
                $check_obj = new entity('sys_seo');
                $check_obj->uid = $value[0];
                $check_obj->pageType = $value[1];

                if( !$this->dbca->fetch( $check_obj ) ){
                    $errorNum++;
                }

                $deleteObjArr[] = $check_obj;

            }

            if( $errorNum > 0 ){

                return array('message'=>'所选部分信息已不存在，请刷新页面' , 'code'=>400 );

            }

            try{

                //开启事务
                $this->dbca->beginTransaction();

                //循环删除
                foreach( $deleteObjArr as $delobj ){

                    $this->dbca->delete( $delobj );

                }

                //提交事务
                $this->dbca->commit();

                return array('message'=>'删除成功', 'code'=>200);

            }catch( Exception $e ){

                $this->dbca->rollBack();
                error_log($e->getMessage());
                return array('message'=>'系统错误，请与管理员联系', 'code'=>500);

            }

        }else{

            return array('message'=>'请选择要删除的seo' , 'code'=>400 );

        }

    }

    /**
     * 生成前台页面seo数据
     * @param string $prefix title前缀
     * @param int $pageType 页面代码（1.首页 2.列表页 3.详情页）[默认为空]
     * @param string $uid uid字段[默认为空]
     * @return array
     */
    public function shopSeo($prefix, $pageType = null, $uid = null) {

        if (!empty($pageType) || !empty($uid)) {

            $arg_array = array(
                'uid' => $uid,
                'pageType' => $pageType,
                'useFlg' => 1
            );
            $temp_seo_array = $this->searchSeoInfo( $arg_array );

        } else {

            $temp_seo_array = null;

        }

        if (!$temp_seo_array) {
            $temp_seo_array = array(
                'title' => null,
                'keywords' => null,
                'description' => null,
            );
        } else {
            $temp_seo_array = $temp_seo_array[0];
        }

        $seo_array = array(

            'title' => empty($temp_seo_array['title']) ? G_SEO_TITLE : $temp_seo_array['title'],
            'keywords' => empty($temp_seo_array['keywords']) ? G_SEO_KEYWORDS : $temp_seo_array['keywords'],
            'description' => empty($temp_seo_array['description']) ? G_SEO_DESCRIPTION : $temp_seo_array['description']

        );

        if (!empty($prefix))
            $seo_array['title'] = $prefix . ' - ' . $seo_array['title'];

        return $seo_array;

    }

	/**
     * 查询配送车辆信息
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *		'rp' => int 分页页数
     * )
	*/
    public function searchVehicleInfo( &$arg_array ){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['vehicl_id']) && is_numeric($arg_array['vehicl_id'])) {
            $sql .= ' AND vehicl_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['vehicl_id'];
        }
		
        if (isset($arg_array['vehicle_code']) && !empty($arg_array['vehicle_code'])) {
            $sql .= ' AND vehicle_code = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['vehicle_code']);
        }

        if (isset($arg_array['plateNumber']) && !empty($arg_array['plateNumber'])) {
            $sql .= ' AND plateNumber = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['plateNumber']);
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_vehicle ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_vehicle ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'vehicle_id'    => $item->vehicle_id,
                    'plateNumber'   => $item->plateNumber,
                    'vehicle_code'  => $item->vehicle_code,
                    'linkman'    	=> $item->linkman,
                    'mobile'       	=> $item->mobile,
                    'phone'    		=> $item->phone,
                    'useFlg'      	=> $item->useFlg
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;


        }

    }
    
    /**
     * 添加OR编辑配送车辆信息
     *
     * @param unknown_type $arg_array
     */
    public function saveVehicleInfo( &$arg_array ){
    	
    	//实例化对象
		$commHandler = new commHandler();
    	
		//获取参数
		$vehicle_id   = isset($arg_array['vehicle_id']) ? trim($arg_array['vehicle_id']) : null;
        $plateNumber   = isset($arg_array['plateNumber']) ? trim($arg_array['plateNumber']) : null;
		$vehicle_code = isset($arg_array['vehicle_code']) ? trim($arg_array['vehicle_code']) : null;
		$linkman      = isset($arg_array['linkman']) ? trim($arg_array['linkman']) : null;
		$mobile       = isset($arg_array['mobile']) ? trim($arg_array['mobile']) : null;
		$phone        = isset($arg_array['phone']) ? trim($arg_array['phone']) : null;
		$useFlg       = isset($arg_array['useFlg']) ? trim($arg_array['useFlg']) : 1;
		
		$error_msg = '';
    	 
		if(!empty($vehicle_id)){
			if(!is_numeric($vehicle_id))
				$error_msg .= '配送号参数错误<br/>';
		}
		
		if(empty($vehicle_code))
			$error_msg .= '配送车编号不能为空<br/>';
			
		if(!empty($mobile)){
			if(!$commHandler->isMobile($mobile))
				$error_msg .= '请填写正确的手机号码<br/>';
		}
		
		if(!empty($phone)){
			if(!$commHandler->isPhone($phone))
				$error_msg .= '请填写正确的电话号码<br/>';
		}
			
		if(!in_array($useFlg, array(0, 1)))
			$error_msg .= '使用状态不正确<br/>';
			
		if(!empty($error_msg))
			return array('code' => 400, 'message' => $error_msg);
    	 	
    	//查询vehicle_code是否存在
		$unique_code = $this->dbca->stmt_query('SELECT vehicle_id FROM sys_vehicle WHERE vehicle_code = ?', 's', $vehicle_code);	
    	 	
    	$sys_vehicle = new entity('sys_vehicle');
    	if(!empty($vehicle_id)){
    		$sys_vehicle->vehicle_id = $vehicle_id;
    		if(!$this->dbca->fetch($sys_vehicle))
    			return array('code' => 404, 'message' => '配送车辆信息未找到');
    		
    		if(isset($unique_code[0]->vehicle_id) && !empty($unique_code[0]->vehicle_id)){
    			if($unique_code[0]->vehicle_id != $sys_vehicle->vehicle_id)
    				return array('code' => 404, 'message' => sprintf('配送车编号「%s」已存在', $vehicle_code));
    		}

    	}else{
    		if(isset($unique_code[0]->vehicle_id) && !empty($unique_code[0]->vehicle_id))
    			return array('code' => 404, 'message' => sprintf('配送车编号「%s」已存在', $vehicle_code));
    		
    		$sys_vehicle->creator = $arg_array['creator'];
    	}	
    		
    	$sys_vehicle->vehicle_code = $vehicle_code;
        $sys_vehicle->plateNumber = $plateNumber;
    	$sys_vehicle->linkman      = $linkman;
    	$sys_vehicle->mobile       = $mobile;
    	$sys_vehicle->phone        = $phone;
    	$sys_vehicle->useFlg       = $useFlg;
    	
    	$this->dbca->save($sys_vehicle);
    	
    	if(!empty($vehicle_id))
    		return array('code' => 200, 'message' => '配送车信息更新成功');
    	else 
    		return array('code' => 200, 'message' => '配送车信息添加成功');	
    }
    
    /**
     * 删除配送车信息
     * @param  参数数组
     */
    function delVehicleInfo(&$arg_array){
    	$delData = $arg_array['delArr'];
    	
    	if(!$delData || !is_array($delData))
    		return array('code' => 400, 'message' => '参数错误');
    		
    	foreach ($delData as $d_val){
    		$this->dbca->stmt_query('DELETE FROM sys_vehicle WHERE vehicle_id = ?', 'i', $d_val);
    	}
    	
    	return array('code' => 200, 'message' => '配送车信息删除成功');
    }
    
    /**
     * 配送网点列表
     *
     * @param unknown_type $arg_array
     */
    function searchBranchInfo(&$arg_array){
    	 //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['branch_id']) && is_numeric($arg_array['branch_id'])) {
            $sql .= ' AND branch_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['branch_id'];
        }

        if (isset($arg_array['vehicle_id']) && is_numeric($arg_array['vehicle_id'])) {
            $sql .= ' AND vehicle_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['vehicle_id'];
        }
		
        if (isset($arg_array['branch_name']) && !empty($arg_array['branch_name'])) {
            $sql .= ' AND branch_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['branch_name']) . '%';
        }
        
        if (isset($arg_array['branch_code']) && !empty($arg_array['branch_code'])) {
        	$sql .= ' AND branch_code = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['branch_code']);
        }

        if (isset($arg_array['provinceid']) && is_numeric($arg_array['provinceid'])) {
            $sql .= ' AND provinceid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['provinceid'];
        }

        if (isset($arg_array['cityid']) && is_numeric($arg_array['cityid'])) {
            $sql .= ' AND cityid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityid'];
        }

        if (isset($arg_array['countyid']) && is_numeric($arg_array['countyid'])) {
            $sql .= ' AND countyid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['countyid'];
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_branch ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_branch ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'branch_id'     => $item->branch_id,
                    'branch_name'   => $item->branch_name,
                    'branch_code'   => $item->branch_code,
                    'principal'    	=> $item->principal,
                    'provinceName'  => $item->provinceName,
                    'cityName'    	=> $item->cityName,
                    'countyName'    => $item->countyName,
                    'address'    	=> $item->address,
                    'mobile'       	=> $item->mobile,
                    'phone'    		=> $item->phone,
                    'logo'    		=> $item->logo ? G_IMAGE_URL . '/' . $item->logo : '',
                    'vehicle_id'    => $item->vehicle_id,
                    'useFlg'      	=> $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }
    
    /**
     * 添加OR编辑配送网点
     *
     */
    function saveBranchInfo(&$arg_array){
    	//实例化对象
		$commHandler = new commHandler();
    	
		//获取参数
		$branch_id    = isset($arg_array['branch_id']) ? trim($arg_array['branch_id']) : null;
		$vehicle_id   = isset($arg_array['vehicle_id']) ? trim($arg_array['vehicle_id']) : null;
		$branch_name  = isset($arg_array['branch_name']) ? trim($arg_array['branch_name']) : null;
		$branch_code  = isset($arg_array['branch_code']) ? trim($arg_array['branch_code']) : null;
		$principal    = isset($arg_array['principal']) ? trim($arg_array['principal']) : null;
		$logoImg      = isset($arg_array['logoImg']) ? trim($arg_array['logoImg']) : null;
		$mobile       = isset($arg_array['mobile']) ? trim($arg_array['mobile']) : null;
		$phone        = isset($arg_array['phone']) ? trim($arg_array['phone']) : null;
		$provinceName = isset($arg_array['pName']) ? trim($arg_array['pName']) : null;
		$cityName     = isset($arg_array['cName']) ? trim($arg_array['cName']) : null;
		$countyName   = isset($arg_array['tName']) ? trim($arg_array['tName']) : '';
		$provinceid   = isset($arg_array['provinceid']) ? trim($arg_array['provinceid']) : null;
		$cityid       = isset($arg_array['cityid']) ? trim($arg_array['cityid']) : null;
		$countyid     = isset($arg_array['countyid']) ? trim($arg_array['countyid']) : 0;
		$address      = isset($arg_array['address']) ? trim($arg_array['address']) : null;
		$useFlg       = isset($arg_array['useFlg']) ? trim($arg_array['useFlg']) : 1;
		
		$error_msg = '';
    	 
		if(!empty($branch_id)){
			if(!is_numeric($branch_id))
				$error_msg .= '网点号参数错误<br/>';
		}
		
		if(!is_numeric($vehicle_id))
			$error_msg .= '配送车辆号参数错误<br/>';
			
		if(empty($branch_name))
			$error_msg .= '网点名称不能为空<br/>';	
			
		if(empty($branch_code))
			$error_msg .= '网点编号不能为空<br/>';		
			
		if(!empty($mobile)){
			if(!$commHandler->isMobile($mobile))
				$error_msg .= '请填写正确的手机号码<br/>';
		}
		
		if(!empty($phone)){
			if(!$commHandler->isPhone($phone))
				$error_msg .= '请填写正确的电话号码<br/>';
		}
		
		if(!is_numeric($provinceid) || !is_numeric($cityid) || empty($provinceName) || empty($cityName))
			$error_msg .= '请选择省市信息<br/>';
			
		if(!in_array($useFlg, array(0, 1)))
			$error_msg .= '使用状态不正确<br/>';
			
		if(!empty($error_msg))
			return array('code' => 400, 'message' => $error_msg);
	
    	//查询brand_code是否存在
		$unique_code = $this->dbca->stmt_query('SELECT branch_id FROM sys_branch WHERE branch_code = ?', 's', $branch_code);	
    	 	
    	$sys_branch = new entity('sys_branch');
    	if(!empty($branch_id)){
    		$sys_branch->branch_id = $branch_id;
    		if(!$this->dbca->fetch($sys_branch))
    			return array('code' => 404, 'message' => '配送网点信息未找到');
    		
    		if(isset($unique_code[0]->branch_id) && !empty($unique_code[0]->branch_id)){
    			if($unique_code[0]->branch_id != $sys_branch->branch_id)
    				return array('code' => 404, 'message' => sprintf('配送网点编号「%s」已存在', $branch_code));
    		}

    	}else{
    		if(isset($unique_code[0]->branch_id) && !empty($unique_code[0]->branch_id))
    			return array('code' => 404, 'message' => sprintf('配送网点编号「%s」已存在', $branch_code));
    		
    		$sys_branch->creator = $arg_array['creator'];
    	}	
    	
    	//图片
		$logo = '';
		if(!empty($logoImg)){
			$imgArr = explode('.', $logoImg);
			$exisDir = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/branch/';
			$exisFile = $exisDir . $branch_code . '.' . $imgArr[1];
			$logo = $branch_code . '.' . $imgArr[1];
			if(!file_exists($exisDir))
				mkdir($exisDir, 0777);
				
			$tmpDir = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/tmp/';
			$tmpFile = $tmpDir . $logoImg;
			if(file_exists($tmpFile)){
				if(file_exists($exisFile))
					@unlink($exisFile);
					
				if(isset($sys_branch->logo) && !empty($sys_branch->logo))
					@unlink($exisDir . $sys_branch->logo);
					
				rename($tmpFile, $exisFile);
			}else{
				if($sys_branch->logo != $logo)
					rename($exisDir . $sys_branch->logo, $exisFile);
			}	
		}
    		
    	$sys_branch->branch_name  = $branch_name;
    	$sys_branch->branch_code  = $branch_code;
    	$sys_branch->principal    = $principal;
    	$sys_branch->address      = $address;
    	$sys_branch->phone        = $phone;
    	$sys_branch->mobile       = $mobile;
    	$sys_branch->logo         = 'branch/' . $logo;
    	$sys_branch->provinceName = $provinceName;
    	$sys_branch->cityName     = $cityName;
    	$sys_branch->countyName   = $countyName;
    	$sys_branch->provinceid   = $provinceid;
    	$sys_branch->cityid       = $cityid;
    	$sys_branch->countyid     = $countyid;
    	$sys_branch->useFlg       = $useFlg;
    	$sys_branch->vehicle_id   = $vehicle_id;
    	
    	$this->dbca->save($sys_branch);
    	
    	if(!empty($branch_id))
    		return array('code' => 200, 'message' => '配送网点信息更新成功');
    	else 
    		return array('code' => 200, 'message' => '配送网点信息添加成功');	
    }
    
    /**
     * 删除网点信息
     *
     */
    function delBranchInfo(&$arg_array){
    	$delData = $arg_array['delArr'];
    	
    	if(!$delData || !is_array($delData))
    		return array('code' => 400, 'message' => '参数错误');
    		
    	foreach ($delData as $d_val){
    		$this->dbca->stmt_query('DELETE FROM sys_branch WHERE branch_id = ?', 'i', $d_val);
    	}
    	
    	return array('code' => 200, 'message' => '配送网点信息删除成功');
    }
}